package com.kun.portal.webframe.controller;

import com.kun.portal.webframe.dao.EquipmentDao;
import com.kun.portal.webframe.dao.MaintenancePlanSettingDao;
import com.kun.portal.webframe.dao.RepairPlaneDao;
import com.kun.portal.webframe.dao.RepairPlaneSettingDao;
import com.kun.portal.webframe.po.*;
import com.kun.portal.webframe.service.AppointmentService;
import com.kun.portal.webframe.service.EquipmentService;
import com.kun.portal.webframe.service.FlightPlaneService;
import com.kun.portal.webframe.util.LazyIgnore;
import com.kun.portal.webframe.util.UserSessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/7/6.
 */

@Controller
///api/equipment/query
@RequestMapping({"/api/equipment"})
public class EquipmentController {
    private static final Logger LOG = LoggerFactory.getLogger(FwUserController.class);
    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private FlightPlaneService flightPlaneService;
    @Autowired
     private  MaintenancePlanSettingDao maintenancePlanSettingDao;
    @Autowired
    private RepairPlaneSettingDao repairPlaneSettingDao;
    @InitBinder
    public void intDate(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
    @RequestMapping(
            value = {"/delete"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void delete(@RequestBody EquipmentPo equipmentPo, HttpSession session) {
        equipmentDao.delete(equipmentPo);
    }

    @RequestMapping(
            value = {"/save"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public EquipmentPo save(@RequestBody EquipmentPo equipmentPo, HttpSession session) {
        return equipmentDao.save(equipmentPo);
    }

    @RequestMapping(
            value = {"/query"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    @Transactional
    public Page<EquipmentPo> query(@RequestParam (required = false) String filter,String status,Pageable pageable, HttpSession session) {

        return equipmentService.find(filter,status,pageable);
//        Page<EquipmentPo> page=  equipmentDao.findAll(pageable);
////        try {
////            List<EquipmentPo> list =  page.getContent();
////            Set<MaintenancePlanSettingPo> set = list.get(0).getMaintenancePlanSettings();
////
////            System.out.print(set);
////        }catch (Exception e){
////            e.printStackTrace();
////        }
//        return  page.getContent();
    }
    @RequestMapping(
            value = {"/queryAll"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    @Transactional
    public List<EquipmentPo> query(HttpSession session) {
        return equipmentDao.findAll();
//        Page<EquipmentPo> page=  equipmentDao.findAll(pageable);
////        try {
////            List<EquipmentPo> list =  page.getContent();
////            Set<MaintenancePlanSettingPo> set = list.get(0).getMaintenancePlanSettings();
////
////            System.out.print(set);
////        }catch (Exception e){
////            e.printStackTrace();
////        }
//        return  page.getContent();
    }

    @RequestMapping(
            value = {"/saveMaintenancePlanSetting"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public MaintenancePlanSettingPo saveMaintenancePlanSetting(@RequestBody MaintenancePlanSettingPo maintenancePlanSetting, HttpSession session) {
        return maintenancePlanSettingDao.save(maintenancePlanSetting);
    }

    @RequestMapping(
            value = {"/deleteMaintenancePlanSetting"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void deleteMaintenancePlanSetting(@RequestBody MaintenancePlanSettingPo maintenancePlanSettingPo, HttpSession session) {
        maintenancePlanSettingDao.delete(maintenancePlanSettingPo);
    }

    @RequestMapping(
            value = {"/saveRepairPlane"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public RepairPlaneSettingPo saveRepairPlane(@RequestBody RepairPlaneSettingPo repairPlaneSettingPo, HttpSession session) {
        return repairPlaneSettingDao.save(repairPlaneSettingPo);
    }

    @RequestMapping(
            value = {"/deleteRepairPlane"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void deleteRepairPlane(@RequestBody RepairPlaneSettingPo repairPlaneSettingPo, HttpSession session) {
        repairPlaneSettingDao.delete(repairPlaneSettingPo);
    }

}
